package com.zhuiyun.pm.utils;

import com.zhuiyun.pm.config.SpringContextUtil;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ShoetMessageUtils
 * @Description 发送手机验证码
 * @Author hbx
 * @Date 2022/10/11 20:15
 **/
public class ShortMessageUtils {

    private static RedisUtil redisUtil = SpringContextUtil.getBean(RedisUtil.class);

    /**
    * @Description 向手机发送验证码 并存到redis
    * @Author lzy
    * @Date 2022/10/17 8:56
    * @Return void
    * @param phone
     * @param manner 验证码的方式
    */
    public static void sendPhoeRedis(String phone,String manner) {
        String host = "http://smsyun01.market.alicloudapi.com";
        String path = "/sms/sms02";
        String method = "POST";
        String appcode = "f97fc486f7204fffa22acb4938c4a48d";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        String code = "147258";
        querys.put("content", "【云信】您的验证码是：" + code);
        redisUtil.set(phone + manner, code, 300);
        querys.put("mobile", phone);
        String bodys = "";
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
    * @Author hbx
    * @Description 向手机号发送验证码
    * @Date 2022/10/11 20:16
    * @param phone
    */
    public static void sendPhoe(String phone) {
        String host = "http://smsyun01.market.alicloudapi.com";
        String path = "/sms/sms02";
        String method = "POST";
        String appcode = "f97fc486f7204fffa22acb4938c4a48d";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        String code = "147258";
        querys.put("content", "【云信】您的验证码是：" + code);
        querys.put("mobile", phone);
        String bodys = "";
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
